package org.dtrd.modules.record.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.common.util.DateUtils;
import org.dtrd.modules.patient.entity.bean.BasePatientInfo;
import org.dtrd.modules.patient.service.IDtrdEntRdPatientService;
import org.dtrd.modules.record.entity.bean.PatientShortInfo;
import org.dtrd.modules.record.entity.po.DtrdViewRlPatientSugarValue;
import org.dtrd.modules.record.mapper.DtrdViewRlPatientSugarValueMapper;
import org.dtrd.modules.record.service.IDtrdViewRlPatientSugarValueService;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author QR
 * @since 2022-04-18
 */
@DS("multi-datasource1")
@Service
public class DtrdViewRlPatientSugarValueServiceImpl extends ServiceImpl<DtrdViewRlPatientSugarValueMapper, DtrdViewRlPatientSugarValue> implements IDtrdViewRlPatientSugarValueService {

    @Autowired
    private IDtrdEntRdPatientService patientService;

    /**
     * 今日测量
     *
     * @param doctorId
     * @param orgCode
     * @return
     */
    @Override
    public List<PatientShortInfo> getMeasurePatientList(@Nullable Integer doctorId, @Nullable String orgCode, @Nullable String recordDate) {
        List<PatientShortInfo> list = baseMapper.getMeasurePatientList(doctorId, orgCode, recordDate);
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(item -> {
                if (StrUtil.isNotEmpty(item.getBirthday())) {
                    item.setAge(DateUtil.age(DateUtils.str2Date(item.getBirthday(), DateUtils.date_sdf.get()), null));
                }
            });
        }
        return list;
    }

    @Override
    public List<PatientShortInfo> getMeasurePatientListV2(List<Integer> teamDoctorIdList, @Nullable String orgCode, @Nullable String recordDate) {
        List<PatientShortInfo> list = baseMapper.getMeasurePatientListV2(teamDoctorIdList, orgCode, recordDate);
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(item -> {
                if (StrUtil.isNotEmpty(item.getBirthday())) {
                    item.setAge(DateUtil.age(DateUtils.str2Date(item.getBirthday(), DateUtils.date_sdf.get()), null));
                }
            });
        }
        return list;
    }

    @Override
    public IPage<PatientShortInfo> getMeasurePatientListV3(List<Integer> teamDoctorIdList, @Nullable String orgCode, @Nullable String recordDate, Integer pageNum, @Nullable Integer pageSize) {
        IPage<PatientShortInfo> page = new Page<>(pageNum, pageSize);
        page = baseMapper.getMeasurePatientListV3(page, teamDoctorIdList, orgCode, recordDate);
        page.convert(item -> {
            if (StrUtil.isNotEmpty(item.getBirthday())) {
                item.setAge(DateUtil.age(DateUtils.str2Date(item.getBirthday(), DateUtils.date_sdf.get()), null));
            }
            BasePatientInfo baseInfo = patientService.getPatientBaseInfo(item.getPatientId());
            if (baseInfo != null) {
                item.setAvatar(baseInfo.getPatientAvatar());
                item.setMobile(baseInfo.getPatientMobile());
                item.setDescription(baseInfo.getPatientsDescription());
                item.setDiabetesType(baseInfo.getDiabetesType());
                item.setDiabetesTypeName(baseInfo.getPatientsDiabetesTypeName());
                item.setConfirmDate(baseInfo.getPatientsConfirmedDate());
            }
            return item;
        });
        return page;
    }

}
